<?php
class PermissionsController extends AppController {

	var $name = 'Permissions';
	var $uses = array('Permission');
	var $helpers = array('Html', 'Form', 'Javascript','Tinymce', 'Time');
	var $components = array('General', 'Auth');
	var $layout = 'admin';
	
	function beforeFilter()
	{
		parent::beforeFilter();
		
		$this->Auth->allow('*'); // allow everybody access/ comment it for permission implement
		
		//$this->Auth->allow('redirectLogin');
		//Change the default error message displayed when someone does not successfully log in.
		$this->Auth->loginError = "Tài khoản hoặc mật khẩu chưa đúng. Xin mời bạn hãy nhập lại!";
	}
	
	function index() {
		$condition = array('Permission.name <>' => '*');
		if (!empty($this->passedArgs))
		{
			if (isset($this->passedArgs['Search.name']))
			{
				$condition[]["Permission.name LIKE"] = "%{$this->passedArgs['Search.name']}%";
				$this->data['Search']['name'] = $this->passedArgs['Search.name'];
			}
			if (isset($this->passedArgs['Search.permission']))
			{
				$condition[]["Permission.name LIKE"] = "{$this->passedArgs['Search.permission']}%";
				$this->data['Search']['permission'] = $this->passedArgs['Search.permission'];
			}
		}
		
		$this->Permissions->recursive = 1;
		$this->paginate = array('limit' => 10, 'page' => 1, 'order' => array('Permission.created' => 'desc'));
		$permissions = $this->paginate('Permission', $condition);
		$this->set('permissions', $permissions);
		
		//Load app/config/permission.php
		Configure::load('permission');
		$this->set('controllers', Configure::read('Controller'));
		$this->set('actions', Configure::read('Action'));
	}
	
	function add()
	{
		if (!empty($this->data))
		{
			$this->data['Permission']['name'] = $this->data['Permission']['permission'] . ':' . $this->data['Permission']['action'];
			if ($this->Permission->save($this->data))
			{
				$this->Session->setFlash('Permission đã được thêm');
			} else {
				$this->Session->setFlash('Lưu thông tin thất bại');
			}
			$this->redirect($this->referer());
		}
	}
	
	function edit($id = null)
	{
		//Check id
		if (!$id){
			$this->Session->setFlash('Không đúng ID');
		}
		
		//Load app/config/permission.php
		Configure::load('permission');
		$this->set('controllers', Configure::read('Controller'));
		$this->set('actions', Configure::read('Action'));
		
		//Check data submit
		if (!empty($this->data))
		{
			$this->Permission->id = $id;
			$this->data['Permission']['name'] = $this->data['Permission']['permission'] . ':' . $this->data['Permission']['action'];
			if ($this->Permission->save($this->data))
			{
				$this->Session->setFlash('Permission đã được sửa');
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash('Lưu thông tin thất bại');
			}
			$this->redirect(array('action' => 'index'));
		}
		
		//Get data by id
		if (empty($this->data)){
			$this->data = $this->Permission->findById($id);
			//Tach quyen
			if (isset($this->data['Permission']))
			{
				$tmp = split(':', $this->data['Permission']['name']);
				if (count($tmp) > 1)
				{
					$this->data['Permission']['permission'] = $tmp[0];
					$this->data['Permission']['action'] = $tmp[1];
				}
			}
		}
		
	}
	
	function delete($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash('Không đúng ID');
			$this->redirect ('index');
		}
		if ($this->Permission->del($id))
		{
			$this->Session->setFlash('Đã xóa xong');
		}
		$this->redirect('index');
	}
	
}
?>